package com.keywaysoft.workflow.controller;

import com.keywaysoft.workflow.service.IProcessImage;
import com.keywaysoft.workflow.utils.R;
import com.keywaysoft.workflow.vo.VariableInstanceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author zhaojx
 */
@Api(tags = "历史相关任务")
@RestController
@RequestMapping("/history")
@Slf4j
public class HistoryController {

    @Autowired
    IProcessImage processImage;

    @GetMapping(value = "/getInstancesByUserName")
    @ApiOperation(value = "获取用户已办历史任务", notes = "获取用户已办历史任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userkey", value = "用户key", dataType = "String", paramType = "query",required = true),
            @ApiImplicitParam(name = "key", value = "流程定义key", dataType = "String", paramType = "query")
    })
    public R<List<HistoricTaskInstance>> InstancesByUser(@RequestParam String userkey,
                                                         @RequestParam(required = false) String key) {
        return processImage.InstancesByUser(userkey,key);
    }

    @GetMapping(value = "/getInstancesByPiID")
    @ApiOperation(value = "获取任务实例历史记录", notes = "获取任务实例历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "piID", value = "实例ID", dataType = "String", paramType = "query",required = true)
    })
    public R<List<HistoricTaskInstance>> getInstancesByPiID(@RequestParam("piID") String piID) {
        return processImage.getInstancesByPiID(piID);
    }

    /**
     * 我发起的记录
     *
     * @param userKey
     * @return
     */
    @GetMapping(value = "/process/mys")
    @ApiOperation(value = "我发起的记录", notes = "我发起的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userKey", value = "用户key", dataType = "String", paramType = "query",required = true)
    })
    public R<List<HistoricProcessInstance>> myProcessStarted(@RequestParam("userKey") String userKey) {
        return processImage.myProcessStarted(userKey);
    }

    /**
     * 功能描述:查询流程历史变量
     */
    @GetMapping(value = "/variable")
    @ApiOperation(value = "查询流程历史变量", notes = "查询流程历史变量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "实例ID", dataType = "String", paramType = "query",required = true)
    })
    public R<List<VariableInstanceVO>> historicVariable(String processInstanceId) {
        return processImage.historicVariable(processInstanceId);
    }

    @GetMapping("/gethighLine")
    @ApiOperation(value = "流程图高亮", notes = "流程图高亮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "instanceId", value = "实例ID", dataType = "String", paramType = "query",required = true),
            @ApiImplicitParam(name = "userKey", value = "用户key", dataType = "String", paramType = "query",required = true)
    })
    public R<Map<String, Object>> gethighLine(@RequestParam("instanceId") String instanceId,
                         @RequestParam("userkey") String userkey) {
        return processImage.gethighLine(instanceId, userkey);
    }


}
